<?php
/**
 * Copyright (C) 2010-2013 Oscar Valenzuela B. (ovalenzuela@xpertians.com)
 * 
 * This file is part of WarpIT.
 * WarpIT is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * WarpIT is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with WarpIT.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * This class cointains HSS Requests methods for WarpIT
 * @author Oscar Valenzuela <ovalenzuela@xpertians.com>
 * @version 1.0
 * @package WarpIT!
 * @copyright Copyright (C) 2010-2013 Xpertians SpA
 * @license GNU GPLv3 or later
 * @link http://www.xpertians.com
 */
 
Class Request_HSS{
	/**
	 * HSS Soap URL
	 *
	 * @var string
	 */
	private $baseUrl;
	
	/**
	 * HTTP Agent Description
	 *
	 * @var string
	 */
	private $userAgent;

	/**
	 * User Login
	 *
	 * @var string
	 */
	private $userLogin;
	
	/**
	 * Password Login
	 *
	 * @var string
	 */
	private $passLogin;
	
	/**
	 * Curl
	 *
	 * @var mixed
	 */
	private $ch;
	
	/**
	 * Proxy information
	 *
	 * @var string
	 */
	private $proxyServer;
	
	/**
	 * Curl responses
	 *
	 * @var mixed
	 */
	private $response;
	
	/**
	 * Request_HSS is the constructor of this class
	 *
	 * Following syntax are recognized :
	 *
	 * @param string $baseUrl Url conformed by IP and Port for HSS Soap
	 * @param string $userLogin User Login
	 * @param string $passLogin Password Login
	 * @param string $proxyServer Proxy information if is needed
	 */
	function Request_HSS($baseUrl, $userLogin, $passLogin, $proxyServer){
		$this->response		= array();
		$this->userAgent	= "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)";
		$this->baseUrl		= $baseUrl;
		$this->userLogin	= $userLogin;
		$this->passLogin	= $passLogin;
		$this->proxyServer	= $proxyServer;
		
		$this->ch		= curl_init();
		curl_setopt($this->ch, CURLOPT_HEADER, 0);
		curl_setopt($this->ch, CURLOPT_POST, 0);
		curl_setopt($this->ch, CURLOPT_USERAGENT, $this->userAgent);
		curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0);
		
		/* Not used in this class
		curl_setopt($this->ch, CURLOPT_USERPWD, $this->userLogin.":".$this->passLogin);
		curl_setopt($this->ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
		*/
		
		curl_setopt($this->ch, CURLOPT_TIMEOUT, 10);
		if($this->proxyServer!=""){
			curl_setopt($this->ch, CURLOPT_PROXY, "http://".$this->proxyServer);
			$arrPort	= explode(":", $this->proxyServer);
			curl_setopt($this->ch, CURLOPT_PROXYPORT, $arrPort[1]);
		}
	}
	
	/**
	 * Request_LGI Login to HSS using Soap
	 *
	 * Following syntax are recognized :
	 *
	 * @return string
	 */
	function Request_LGI(){
		curl_setopt($this->ch, CURLOPT_URL, $this->baseUrl);

		/*	Soap Raw XML Template	*/
		$xml_post_string	= '
			<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:lgi="http://www.huawei.com/HLR9820/LGI">
			<soapenv:Header/>
			<soapenv:Body>
			<lgi:LGI>
			<lgi:OPNAME>'.$this->userLogin.'</lgi:OPNAME>
			<lgi:PWD>'.$this->passLogin.'</lgi:PWD>
			<lgi:HLRSN>1</lgi:HLRSN>
			</lgi:LGI>
			</soapenv:Body>
			</soapenv:Envelope>';

		/*	Soap Headers Template	*/
		$headers = array(
			"Content-type: text/xml;charset=\"utf-8\"",
			"Accept: text/xml",
			"Cache-Control: no-cache",
			"Pragma: no-cache",
			"Content-length: ".strlen($xml_post_string),
		);
		
		curl_setopt($this->ch, CURLOPT_POSTFIELDS, $xml_post_string);
		curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
		
		$this->response['body']		= curl_exec($this->ch);
		$this->response['header']	= curl_getinfo($this->ch);
		
		return $this->response;
	}
	
	/**
	 * Request_URL return LGI url
	 *
	 * Following syntax are recognized :
	 *
	 * @return string
	 */
	function Request_URL(){
		return $this->response['header']['url'];
	}
	
	/**
	 * Request_LST_SUB Ask HSS for SUB properties
	 *
	 * Following syntax are recognized :
	 *
	 * @param string $ISDN Identification Suscription Number
	 * @return mixed
	 *
	 */
	function Request_LST_SUB($ISDN){
		curl_setopt($this->ch, CURLOPT_URL, $this->response['header']['url']);

		/*	Soap Raw XML Template	*/
		$xml_post_string	= '
			<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:lst="http://www.huawei.com/HLR9820/LST">
			<soapenv:Header/>
			<soapenv:Body>
			<lst:LST_SUB>
			<lst:ISDN>'.$ISDN.'</lst:ISDN>
			<lst:DETAIL>true</lst:DETAIL>
			</lst:LST_SUB>
			</soapenv:Body>
			</soapenv:Envelope>';
			
		/*	Soap Headers Template	*/
		$headers = array(
			"Content-type: text/xml;charset=\"utf-8\"",
			"Accept: text/xml",
			"Cache-Control: no-cache",
			"Pragma: no-cache",
			"Content-length: ".strlen($xml_post_string),
		);
		
		curl_setopt($this->ch, CURLOPT_POSTFIELDS, $xml_post_string);
		curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
		
		$this->response['body']		= curl_exec($this->ch);
		$this->response['header']	= curl_getinfo($this->ch);
		
		return $this->response;
	}
	
	/**
	 * Request_LST_DYNSUB Ask HSS for DYNSUB properties
	 *
	 * Following syntax are recognized :
	 *
	 * @param string $ISDN Identification Suscription Number
	 * @return mixed
	 *
	 */
	function Request_LST_DYNSUB($ISDN){
		curl_setopt($this->ch, CURLOPT_URL, $this->response['header']['url']);

		/*	Soap Raw XML Template	*/
		$xml_post_string	= '
			<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:lst="http://www.huawei.com/HLR9820/LST_DYNSUB">
			<soapenv:Header/>
			<soapenv:Body>
			<lst:LST_DYNSUB>
			<lst:ISDN>'.$ISDN.'</lst:ISDN>
			</lst:LST_DYNSUB>
			</soapenv:Body>
			</soapenv:Envelope>';
			
		/*	Soap Headers Template	*/
		$headers = array(
			"Content-type: text/xml;charset=\"utf-8\"",
			"Accept: text/xml",
			"Cache-Control: no-cache",
			"Pragma: no-cache", 
			"Content-length: ".strlen($xml_post_string),
		);
		
		curl_setopt($this->ch, CURLOPT_POSTFIELDS, $xml_post_string);
		curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
		
		$this->response['body']		= curl_exec($this->ch);
		$this->response['header']	= curl_getinfo($this->ch);
		
		return $this->response;
	}
	
	/**
	 * Request_LST_CFALL Ask HSS for CFALL properties
	 *
	 * Following syntax are recognized :
	 *
	 * @param string $ISDN Identification Suscription Number
	 * @return mixed
	 *
	 */
	function Request_LST_CFALL($ISDN){
		curl_setopt($this->ch, CURLOPT_URL, $this->response['header']['url']);

		/*	Soap Raw XML Template	*/
		$xml_post_string	= '
			<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:lst="http://www.huawei.com/HLR9820/LST_DYNSUB">
			<soapenv:Header/>
			<soapenv:Body>
			<lst:LST_CFALL>
			<lst:ISDN>'.$ISDN.'</lst:ISDN>
			</lst:LST_CFALL>
			</soapenv:Body>
			</soapenv:Envelope>';
			
		/*	Soap Headers Template	*/
		$headers = array(
			"Content-type: text/xml;charset=\"utf-8\"",
			"Accept: text/xml",
			"Cache-Control: no-cache",
			"Pragma: no-cache",
			"Content-length: ".strlen($xml_post_string),
		);
		
		curl_setopt($this->ch, CURLOPT_POSTFIELDS, $xml_post_string);
		curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
		
		$this->response['body']		= curl_exec($this->ch);
		$this->response['header']	= curl_getinfo($this->ch);
		
		return $this->response;
	}
	
	/**
	 * Request_LST_ECATEGORY Ask HSS for ECATEGORY properties
	 *
	 * Following syntax are recognized :
	 *
	 * @param string $ISDN Identification Suscription Number
	 * @return mixed
	 *
	 */
	function Request_LST_ECATEGORY($ISDN){
		curl_setopt($this->ch, CURLOPT_URL, $this->response['header']['url']);

		/*	Soap Raw XML Template	*/
		$xml_post_string	= '
			<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:lst="http://www.huawei.com/HLR9820/LST_ECATEGORY">
			<soapenv:Header/>
			<soapenv:Body>
			<lst:LST_ECATEGORY>
			<lst:ISDN>'.$ISDN.'</lst:ISDN>
			</lst:LST_ECATEGORY>
			</soapenv:Body>
			</soapenv:Envelope>';
			
		/*	Soap Headers Template	*/
		$headers = array(
			"Content-type: text/xml;charset=\"utf-8\"",
			"Accept: text/xml",
			"Cache-Control: no-cache",
			"Pragma: no-cache",
			"Content-length: ".strlen($xml_post_string),
		);
		
		curl_setopt($this->ch, CURLOPT_POSTFIELDS, $xml_post_string);
		curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
		
		$this->response['body']		= curl_exec($this->ch);
		$this->response['header']	= curl_getinfo($this->ch);
		
		return $this->response;
	}
	
	/**
	 * Request_LST_CWAIT Ask HSS for CWAIT properties
	 *
	 * Following syntax are recognized :
	 *
	 * @param string $ISDN Identification Suscription Number
	 * @return mixed
	 *
	 */
	function Request_LST_CWAIT($ISDN){
		curl_setopt($this->ch, CURLOPT_URL, $this->response['header']['url']);

		/*	Soap Raw XML Template	*/
		$xml_post_string	= '
			<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:lst="http://www.huawei.com/HLR9820/LST_ECATEGORY">
			<soapenv:Header/>
			<soapenv:Body>
			<lst:LST_CWAIT>
			<lst:ISDN>'.$ISDN.'</lst:ISDN>
			</lst:LST_CWAIT>
			</soapenv:Body>
			</soapenv:Envelope>';
			
		/*	Soap Headers Template	*/
		$headers = array(
			"Content-type: text/xml;charset=\"utf-8\"",
			"Accept: text/xml",
			"Cache-Control: no-cache",
			"Pragma: no-cache",
			"Content-length: ".strlen($xml_post_string),
		);
		
		curl_setopt($this->ch, CURLOPT_POSTFIELDS, $xml_post_string);
		curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
		
		$this->response['body']		= curl_exec($this->ch);
		$this->response['header']	= curl_getinfo($this->ch);
		
		return $this->response;
	}
	
	/**
	 * Request_LST_OPTGPRS Ask HSS for OPTGPRS properties
	 *
	 * Following syntax are recognized :
	 *
	 * @param string $ISDN Identification Suscription Number
	 * @return mixed
	 *
	 */
	function Request_LST_OPTGPRS($ISDN){
		curl_setopt($this->ch, CURLOPT_URL, $this->response['header']['url']);

		/*	Soap Raw XML Template	*/
		$xml_post_string	= '
			<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:lst="http://www.huawei.com/HLR9820/LST_OPTGPRS">
			<soapenv:Header/>
			<soapenv:Body>
			<lst:LST_OPTGPRS>
			<lst:ISDN>'.$ISDN.'</lst:ISDN>
			</lst:LST_OPTGPRS>
			</soapenv:Body>
			</soapenv:Envelope>';
			
		/*	Soap Headers Template	*/
		$headers = array(
			"Content-type: text/xml;charset=\"utf-8\"",
			"Accept: text/xml",
			"Cache-Control: no-cache",
			"Pragma: no-cache",
			"Content-length: ".strlen($xml_post_string),
		);
		
		curl_setopt($this->ch, CURLOPT_POSTFIELDS, $xml_post_string);
		curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
		
		$this->response['body']		= curl_exec($this->ch);
		$this->response['header']	= curl_getinfo($this->ch);
		
		return $this->response;
	}
}
?>
